home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch11 / Cycloid2.frm (.txt) < prev    next >
Visual Basic Form  |  1999-06-12  |  6KB  |  209 lines

  1. VERSION 5.00
  2. Begin VB.Form frmCycloid2 
  3.    Caption         =   "Cycloid2"
  4.    ClientHeight    =   5310
  5.    ClientLeft      =   2175
  6.    ClientTop       =   645
  7.    ClientWidth     =   4830
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   5310
  11.    ScaleWidth      =   4830
  12.    Begin VB.TextBox txtXScale 
  13.       Height          =   285
  14.       Left            =   600
  15.       TabIndex        =   9
  16.       Text            =   "2.0"
  17.       Top             =   480
  18.       Width           =   615
  19.    End
  20.    Begin VB.TextBox txtYScale 
  21.       Height          =   285
  22.       Left            =   2160
  23.       TabIndex        =   8
  24.       Text            =   "1.0"
  25.       Top             =   480
  26.       Width           =   615
  27.    End
  28.    Begin VB.TextBox txtTheta 
  29.       Height          =   285
  30.       Left            =   3720
  31.       TabIndex        =   7
  32.       Text            =   "0.0"
  33.       Top             =   480
  34.       Width           =   615
  35.    End
  36.    Begin VB.TextBox txtDt 
  37.       Height          =   285
  38.       Left            =   2160
  39.       TabIndex        =   6
  40.       Text            =   "0.1"
  41.       Top             =   45
  42.       Width           =   615
  43.    End
  44.    Begin VB.TextBox txtTmin 
  45.       Height          =   285
  46.       Left            =   0
  47.       TabIndex        =   4
  48.       Text            =   "0"
  49.       Top             =   45
  50.       Width           =   615
  51.    End
  52.    Begin VB.CommandButton cmdGo 
  53.       Caption         =   "Go"
  54.       Default         =   -1  'True
  55.       Height          =   375
  56.       Left            =   4200
  57.       TabIndex        =   3
  58.       Top             =   0
  59.       Width           =   615
  60.    End
  61.    Begin VB.TextBox txtTmax 
  62.       Height          =   285
  63.       Left            =   1200
  64.       TabIndex        =   2
  65.       Text            =   "6.2832"
  66.       Top             =   45
  67.       Width           =   615
  68.    End
  69.    Begin VB.PictureBox picCanvas 
  70.       AutoRedraw      =   -1  'True
  71.       Height          =   4815
  72.       Left            =   0
  73.       ScaleHeight     =   4755
  74.       ScaleWidth      =   4755
  75.       TabIndex        =   0
  76.       Top             =   840
  77.       Width           =   4815
  78.    End
  79.    Begin VB.Label Label1 
  80.       Caption         =   "X Scale"
  81.       Height          =   255
  82.       Index           =   2
  83.       Left            =   0
  84.       TabIndex        =   12
  85.       Top             =   480
  86.       Width           =   615
  87.    End
  88.    Begin VB.Label Label1 
  89.       Caption         =   "Y Scale"
  90.       Height          =   255
  91.       Index           =   3
  92.       Left            =   1560
  93.       TabIndex        =   11
  94.       Top             =   495
  95.       Width           =   615
  96.    End
  97.    Begin VB.Label Label1 
  98.       Caption         =   "Theta"
  99.       Height          =   255
  100.       Index           =   4
  101.       Left            =   3120
  102.       TabIndex        =   10
  103.       Top             =   495
  104.       Width           =   615
  105.    End
  106.    Begin VB.Label Label1 
  107.       Caption         =   "dt"
  108.       Height          =   255
  109.       Index           =   1
  110.       Left            =   1920
  111.       TabIndex        =   5
  112.       Top             =   60
  113.       Width           =   255
  114.    End
  115.    Begin VB.Label Label1 
  116.       Caption         =   "<= t <="
  117.       Height          =   255
  118.       Index           =   0
  119.       Left            =   645
  120.       TabIndex        =   1
  121.       Top             =   60
  122.       Width           =   495
  123.    End
  124. Attribute VB_Name = "frmCycloid2"
  125. Attribute VB_GlobalNameSpace = False
  126. Attribute VB_Creatable = False
  127. Attribute VB_PredeclaredId = True
  128. Attribute VB_Exposed = False
  129. Option Explicit
  130. Private Const PI = 3.14159265
  131. ' Draw the curve on the indicated picture box,
  132. ' scaled and rotated.
  133. Private Sub DrawCurve(ByVal pic As PictureBox, ByVal start_t As Single, ByVal stop_t As Single, ByVal dt As Single, ByVal x_scale As Single, ByVal y_scale As Single, ByVal theta As Single)
  134. Dim cx As Single
  135. Dim cy As Single
  136. Dim sin_theta As Single
  137. Dim cos_theta As Single
  138. Dim old_x As Single
  139. Dim old_y As Single
  140. Dim new_x As Single
  141. Dim new_y As Single
  142. Dim t As Single
  143.     cx = pic.ScaleLeft + pic.ScaleWidth / 2
  144.     cy = pic.ScaleTop + pic.ScaleHeight / 2
  145.     ' Get Cos(theta) and Sin(theta)
  146.     sin_theta = Sin(theta)
  147.     cos_theta = Cos(theta)
  148.     pic.Cls
  149.     old_x = x_scale * X(start_t)
  150.     old_y = y_scale * Y(start_t)
  151.     pic.CurrentX = cx + old_x * cos_theta - old_y * sin_theta
  152.     pic.CurrentY = cy + old_x * sin_theta + old_y * cos_theta
  153.     t = start_t + dt
  154.     Do While t < stop_t
  155.         old_x = x_scale * X(t)
  156.         old_y = y_scale * Y(t)
  157.         pic.Line -( _
  158.             cx + old_x * cos_theta - old_y * sin_theta, _
  159.             cy + old_x * sin_theta + old_y * cos_theta)
  160.         t = t + dt
  161.     Loop
  162.     old_x = x_scale * X(stop_t)
  163.     old_y = y_scale * Y(stop_t)
  164.     pic.Line -( _
  165.         cx + old_x * cos_theta - old_y * sin_theta, _
  166.         cy + old_x * sin_theta + old_y * cos_theta)
  167. End Sub
  168. ' The parametric function Y(t).
  169. Private Function Y(ByVal t As Single) As Single
  170.     Y = 2000 * (27 * Sin(t) + 15 * Sin(t * 20 / 7)) / 42
  171. End Function
  172. ' The parametric function X(t).
  173. Private Function X(ByVal t As Single) As Single
  174.     X = 2000 * (27 * Cos(t) + 15 * Cos(t * 20 / 7)) / 42
  175. End Function
  176. Private Sub cmdGo_Click()
  177. Dim tmin As Single
  178. Dim tmax As Single
  179. Dim dt As Single
  180. Dim x_scale As Single
  181. Dim y_scale As Single
  182. Dim theta As Single
  183.     tmin = CSng(txtTmin.Text)
  184.     tmax = CSng(txtTmax.Text)
  185.     dt = CSng(txtDt.Text)
  186.     x_scale = CSng(txtXScale.Text)
  187.     y_scale = CSng(txtYScale.Text)
  188.     theta = -CSng(txtTheta.Text) * 180 / PI
  189.     DrawCurve picCanvas, tmin, tmax, dt, x_scale, y_scale, theta
  190. End Sub
  191. Private Sub Form_Load()
  192.     txtTmin.Text = Format$(0, "0.00")
  193.     txtTmax.Text = Format$(14 * PI, "0.00")
  194.     txtDt.Text = "0.1"
  195.     txtXScale.Text = "0.5"
  196.     txtYScale.Text = "1.0"
  197.     txtTheta.Text = "30"
  198. End Sub
  199. Private Sub Form_Resize()
  200. Dim lft As Single
  201. Dim hgt As Single
  202.     lft = txtDt.Left + txtDt.Width
  203.     If lft < ScaleWidth - cmdGo.Width Then lft = ScaleWidth - cmdGo.Width
  204.     cmdGo.Left = lft
  205.     hgt = ScaleHeight - picCanvas.Top
  206.     If hgt < 120 Then hgt = 120
  207.     picCanvas.Move 0, picCanvas.Top, ScaleWidth, hgt
  208. End Sub
  209.